
public class IncDecRement implements Expressao {

	private Expressao expr;
	private String acao;
	private Integer regFinal;
	
	public IncDecRement(Expressao expr, String acao) {
		this.expr = expr;
		this.acao = decodeAcao(acao);
	}

	private String decodeAcao(String acao) {
		if (acao.equals("++")) return "INC";
		if (acao.equals("--")) return "DEC";
		
		return "";
	}

	@Override
	public String getCode() {
		StringBuffer sb = new StringBuffer();

		regFinal = ParserUtils.novoRegistrador();

		String c = expr.getCode();
		
		sb.append("LD R" + regFinal + ", " + c);
		sb.append(System.lineSeparator());

		sb.append(acao + " R" + regFinal + ", R" + regFinal);
		sb.append(System.lineSeparator());

		sb.append("ST " + c + ", R" + regFinal);
		
		ParserUtils.liberaRegistradores(regFinal);
		return sb.toString();
	}

	@Override
	public Integer registradorFinal() {
		return regFinal;
	}

	@Override
	public boolean possuiRegistradorFinal() {
		return true;
	}

}
